#include "MapTest.h"


MapTest::MapTest(CStateManager *state) : IGameState(state)
{

}

MapTest::~MapTest()
{
	Destroy();
}


void MapTest::Init()
{
	
	CResourceManager* res = CResourceManager::GetInstance();
	CEffectManager* effect = CEffectManager::GetInstance();

	res->Init( "Data/Level1.ini" , m_StateManager->m_pGraphic );

	effect->Init( "Data/Level1.ini" );
	//Set up camera
	m_pCamera = new CCamera();
	m_pCamera->SetCameraView( 0 , 1200 , 800 , 600 );

	//Set up new map reader and tile layer
	ReadRaw();

	//Set up new object
	m_pObject = new CPlayer();
	m_pObject->Init("Data/Level1.ini");
	m_pObject->RegisterCallback( CCharacter::CALLBACK_TYPE_COLLISION , CTileLayer::CollisionCheck , m_pTiledLayer );

	CMob* pRockMob;
	pRockMob = new CRocky();
	pRockMob->Init("Data/Level1.ini"  );
	pRockMob->SetMovementRange( 200.0f , 200.0f );
	pRockMob->SetSeekRange( 100.0f , 100.0f );
	pRockMob->RegisterCallback( CCharacter::CALLBACK_TYPE_COLLISION , CTileLayer::CollisionCheck , m_pTiledLayer );
	pRockMob->RegisterCallback( CCharacter::CALLBACK_TYPE_ACTIVE , CPlayer::Active , m_pObject );

	CMob* pBugMob;
	pBugMob = new CBugky();
	pBugMob->Init("Data/Level1.ini"  );
	pBugMob->SetMovementRange( 200.0f , 200.0f );
	pBugMob->SetSeekRange( 100.0f , 100.0f );
	pBugMob->RegisterCallback( CCharacter::CALLBACK_TYPE_COLLISION , CTileLayer::CollisionCheck , m_pTiledLayer );
	pBugMob->RegisterCallback( CCharacter::CALLBACK_TYPE_ACTIVE , CPlayer::Active , m_pObject );

	m_listMob.push_back( pRockMob );
	m_listMob.push_back( pBugMob );
}


void MapTest::Update()
{
	ProcessInput();
	ProcessObject();

	m_pObject->Update(g_DeltaTime);
	
	m_pCamera->UpdateCamera(m_pTiledLayer , m_pObject->GetFixedXPos() , m_pObject->GetYposition());
	
	for( int i = 0 ; i < 2 ; ++i )
	{
		if( m_listMob[i]->IsAlive() )
		{
			m_listMob[i]->Update( g_DeltaTime );
			if( m_pObject->IsCollision( m_listMob[i] ) && m_pObject->IsColliable() && m_listMob[i]->IsColliable() )
			{
				m_pObject->Hurt( m_listMob[i] );
			}
		}
	}
	
	
	
	
	

	CEffectManager::GetInstance()->UpdateEffect( g_DeltaTime );
}


void MapTest::Render()
{
	m_StateManager->m_pGraphic->BeginRender();

	m_StateManager->m_pGraphic->GetSpriteHandler()->Begin(D3DXSPRITE_ALPHABLEND);

	
	m_pTiledLayer->DrawMap( m_pCamera->GetCameraView() , m_StateManager->m_pGraphic->GetSpriteHandler() );

	m_pObject->Render( m_pCamera->GetXpos() , m_pCamera->GetYpos() );

	for( int i = 0 ; i < 2 ; ++i )
	{
		if( m_listMob[i]->IsAlive( ) )
		{
			m_listMob[i]->Render( m_pCamera->GetXpos() , m_pCamera->GetYpos() );
		}
	}

	

	CEffectManager::GetInstance()->DrawEffect(  m_pCamera->GetXpos() , m_pCamera->GetYpos() );

	m_StateManager->m_pGraphic->GetSpriteHandler()->End();

	
	m_StateManager->m_pGraphic->EndRender();

	m_StateManager->m_pGraphic->GetDevice()->Present(NULL , NULL , NULL , NULL );
}

void MapTest::Destroy()
{
	
}

void MapTest::ProcessCamera()
{

}






void MapTest::ReadRaw()
{
	D3DCOLOR** pRawMap;
	ULONG i , j ;

	
	m_pMapReader = new CMapReader();

	m_pMapReader->LoadRawMap("Res/map.png" , m_StateManager->m_pGraphic->GetDevice()  );

	m_pTiledLayer = new CTileLayer( &CResourceManager::GetInstance()->m_pListImage[CResourceManager::IMG_TILED] , m_pMapReader->GetMapWidth() , m_pMapReader->GetMapHeight() , 40 );
	

	pRawMap = m_pMapReader->GetRawMap();

	for ( i = 0 ; i < m_pMapReader->GetMapHeight() ; ++i )
	{
		for ( j = 0 ; j < m_pMapReader->GetMapWidth() ; ++j )
		{
			switch(pRawMap[i][j])
			{
			case D3DCOLOR_ARGB(255,0,0,0):
				m_pTiledLayer->SetCell( i , j , 1 );
				break;
			case D3DCOLOR_ARGB(255,255,0,0):
				m_pTiledLayer->SetCell( i , j , - 1 );
				break;
			}
		}
	}

	m_pMapReader->Release();
}




void MapTest::ProcessInput()
{

	//Check if Key Right was pressed
	if(  GetKeyState( VK_RIGHT ) & 0xff00  )
	{
		m_pObject->Moving( g_DeltaTime , CPlayer::DIR_RIGHT );
	}
	else if( GetKeyState( VK_LEFT ) & 0xff00 ) //Check if Key Left was pressed
	{
		m_pObject->Moving( g_DeltaTime , CPlayer::DIR_LEFT );
	}
	else //If left and right key hadn't pressed and our state is walking , make it stading again , if we are in another state , just pass it
	{
		m_pObject->Standing();
	}
	//Check if Key Up was pressed
	if( GetKeyState( VK_S ) & 0xff00 )
	{
		//If we in landing not falling make it jump
		m_pObject->Jumping();
	}
	else //If we've release the key up then make character falling , otherwise just pass it
	{
		m_pObject->Falling();	
	}

	//Press key D for atking
	if( GetKeyState( VK_D ) & 0xff00 )
	{
		m_pObject->Slashing();
	}

	//Dashing
	if( GetKeyState( VK_A ) & 0xff00 )
	{
		m_pObject->Dashing(g_DeltaTime);
	}
	else
	{
		m_pObject->EndDash();
	}

	static bool flag = false;
	if( GetKeyState( VK_W ) & 0xff00 )
	{
		if( !flag )
		{
			CEffectManager::GetInstance()->StartEffect( CEffectManager::EFF_BURST , m_pObject->GetBoundBox().right - 15 , m_pObject->GetBoundBox().top + 5 , 500.0f , 0.0f , 5.0f );
			flag = true;
		}
	}

	if( GetKeyState( VK_BACK ) & 0xff00 )
	{
		flag = false;
	}

}


void MapTest::ProcessObject()
{
	
}